package com.nuedu.corejava.ch12.homework;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 
 * 2、编写一个java程序实现文件复制功能，要求将d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。 [必做题]
 * @author admin
 *
 */
public class HomeWork2 {
	public static void main(String[] args) {
		String filePath = "d:/io/copysrc.doc";
		Path path = Paths.get( filePath);
		File file = new File(filePath);
		file.getParentFile().mkdirs();
		FileWriter  writer =null;
		try {
			if (file.exists()) {
				file.delete();
				file.createNewFile();
				System.out.println("文件删除成功！并重新创建成功");
			}else {
				file.createNewFile();
				System.out.println("文件创建成功!");
			}
			writer = new FileWriter(file);
			for (int i = 0; i < 12; i++) {
				String str = "我爱你！";
				writer.write(str);
				writer.flush();
				
			}
			Path destPath = Paths.get("d:/io/copydes.doc");
			Files.copy(path, destPath);
			System.out.println("文件拷贝成功");
		} catch (IOException e) {
			
			e.printStackTrace();
		}finally {
			if (writer!=null) {
				try {
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

}
